﻿namespace CloudNinja.Metering.TypeRegistration
{
    using Microsoft.Practices.ServiceLocation;
    using Microsoft.Practices.Unity;

    public class MeteringContainer
    {
        private static readonly object LockObject = new object();

        private static volatile IServiceLocator currentContainer;

        internal static IServiceLocator Current
        {
            get
            {
                if (currentContainer == null)
                {
                    lock (LockObject)
                    {
                        if (currentContainer == null)
                        {
                            currentContainer = new UnityServiceLocator(new UnityContainer());
                        }
                    }
                }

                return currentContainer;
            }
        }

        public static Metering GetMetering()
        {
            var container = Current.GetInstance<IUnityContainer>();

            MeteringContainerBootstrapper.RegisterTypes(container);

            return container.Resolve(typeof(Metering)) as Metering;
        }
    }
}